package com.lonely.web.controller.auth;

import com.lonely.common.core.domain.api.R;
import com.lonely.common.core.domain.auth.LoginBodyDto;
import com.lonely.system.domain.vo.auth.LoginVo;
import com.lonely.system.domain.vo.auth.UserInfoVo;
import com.lonely.system.service.IAuthenticationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 登录验证
 *
 * @author fzzf
 */
@RestController
@RequestMapping("/auth")
@Api(tags = "安全鉴定")
public class AuthenticationController {

    @Autowired
    private IAuthenticationService authenticationService;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public R<LoginVo> login(@Validated @RequestBody LoginBodyDto loginBody) {
        // 生成令牌
        String token = authenticationService.login(loginBody);
        return R.data(new LoginVo().setToken(token).setRefreshToken(token));
    }

    /**
     * 退出
     *
     * @return
     */
    @GetMapping("/logout")
    @ApiOperation("退出")
    public R<Boolean> logout() {
        this.authenticationService.logout();
        return R.data(true);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getUserInfo")
    public R<UserInfoVo> getUserInfo() {
        return R.data(this.authenticationService.getUserInfo());
    }

}
